<?php

namespace app\index\controller;

use app\api\logic\lawyer\SingleOrderLogic;
use app\api\validate\lawyer\OrderValidate;
use app\common\controller\PcUserBase;
use app\common\helper\QrcodeHelper;
use app\common\logic\OrderLogic;
use think\facade\View;

class Order extends PcUserBase
{
    public function add()
    {
        $data = (new OrderValidate())->doCheck('add');
        $data['user_id'] = $this->uid;
        $order= (new SingleOrderLogic())->add($data);
        $this->success(['order_id'=>$order['id']]);
    }
    
    // 订单支付详情
    public function payment()
    {
        $order_id = input('order_id/d');
        $order = \app\common\model\Order::where('id',$order_id)
            ->where('user_id',$this->uid)
            ->find();
        if (!$order) $this->redirect("/");
        View::assign('order',$order);
        return View::fetch();
    }
    
    // 通过订单返回pay_id
    public function orderPay()
    {
        if (!isset($this->data['order_id'])) $this->error();
        if (isset($this->data['bz'])) {
            $order = \app\common\model\Order::where("user_id",$this->uid)
                ->find($this->data['order_id']);
            $order->bz = $this->data['bz'];
            $order->save();
        }
        $pay_id = \app\common\logic\common\PayLogic::createOrUpdatePayOrder($this->data['order_id']);
        $this->success(['pay_id'=>$pay_id]);
    }
    
    public function nativePay()
    {
        $order_id = input("order_id/d");
        if (empty($order_id)) throwError();
        $order = \app\common\model\Order::where("id",$order_id)
            ->where("user_id",$this->uid)->find();
        if (!$order) throwError();
        if ($order->status !== -1) throwError('订单已经支付');
        $this->success([
            "qrcode" => OrderLogic::nativePay($order_id)
        ]);
    }
    
    public function update()
    {
        $order_id = input("order_id/d");
        \app\common\model\Order::update(["bz"=>input('bz')],['id'=>$order_id,"user_id"=>$this->uid]);
        $this->success();
    }
    
    // 生成条形码
    public function getBarcode()
    {
        $order_id = input("order_id/d");
        if (empty($order_id)) throwError();
        OrderLogic::getBarcode($order_id,$this->uid);
    }
    
    //取消订单
    public function cancel()
    {
        $order_id = input("order_id/d");
        if (!$order_id) throwError();
        OrderLogic::cancelOrder($order_id,$this->uid);
        $this->success();
    }
    
    public function createOrder()
    {
        $order = OrderLogic::createOrder($this->data);
        $this->success($order);
    }

    public function orderPre()
    {
        $data = OrderLogic::orderPre($this->data);
        $this->success($data);
    }

    public function getOrderList()
    {
        $data = OrderLogic::getOrderList($this->data);
        $this->success($data);
    }

    public function delOrder()
    {
        OrderLogic::delOrder($this->data);
        $this->success();
    }

    public function setOrderStatus()
    {
        OrderLogic::setOrderStatus($this->data);
        $this->success();
    }

    public function getOrderDetail()
    {
        $order = OrderLogic::getOrderDetail($this->data);
        $this->success($order);
    }

    public function orderConfirm()
    {
        OrderLogic::setOrderStatus([
            "id" => $this->data['id'],
            "user_id" => $this->data['user_id'],
            "status" => 400
        ]);
        $this->success();
    }
}